package com.tanhua.server.controller;

import com.tanhua.common.vo.PageResult;
import com.tanhua.server.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author LOVE
 */
@RestController
@RequestMapping("/smallVideos")
public class VideoController {

    @Autowired
    private VideoService videoService;


    /**
     * 上传视频
     *
     * @param map 视频图片，视频
     * @return
     */
    @PostMapping
    public ResponseEntity<String> saveVideo(@RequestParam Map<String, MultipartFile> map) {
        return videoService.saveVideo(map);
    }

    /**
     * 视频列表显示
     *
     * @return
     */
    @GetMapping
    public ResponseEntity videoListDisplay(Integer page, Integer pagesize) {
        PageResult pageResult = videoService.videoListDisplay(page, pagesize);
        if (pageResult == null) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 视频点赞
     *
     * @param videoId 视频id
     * @return null
     */
    @PostMapping("{id}/like")
    public ResponseEntity videoLikes(@PathVariable("id") String videoId) {
        //进行点赞
        Boolean aBoolean = videoService.videoLikes(videoId);

        //返回是否成功
        return ResponseEntity.ok(aBoolean);
    }

    /**
     * 取消视频点赞
     *
     * @param videoId
     * @return
     */
    @PostMapping("{id}/dislike")
    public ResponseEntity<Boolean> unlikeVideo(@PathVariable("id") String videoId) {
        //进行取消点赞
        Boolean bBoolean = videoService.unlikeVideo(videoId);

        //返回是否成功
        return ResponseEntity.ok(bBoolean);
    }

    /**
     * 获取评论列表
     *
     * @param videoId 视频id
     */
    @GetMapping("{id}/comments")
    public ResponseEntity<PageResult> getAListOfComments(@PathVariable("id") String videoId, Integer page, Integer pagesize) {
        //获取评论列表
        PageResult comments = videoService.getAListOfComments(videoId, page, pagesize);
        //判断评论列表是否为null
        if (comments.getItems() != null) {
            return ResponseEntity.ok(comments);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 发表评论
     *
     * @param videoId 视频id
     * @param map     评论内容
     * @return
     */
    @PostMapping("{id}/comments")
    public ResponseEntity<?> postAComment(@PathVariable("id") String videoId, @RequestBody Map<String, String> map) {
        String comment = map.get("comment");
        //发表评论
        return videoService.postAComment(videoId, comment);

    }

    /**
     * 评论点赞
     *
     * @param videoId 视频id
     */
    @PostMapping("comments/{id}/like")
    public ResponseEntity<Boolean> commentLike(@PathVariable("id") String videoId) {
        //进行点赞
        return ResponseEntity.ok(videoService.commentLike(videoId));
    }

    /**
     * 取消评论点赞
     *
     * @param videoId 视频id
     */
    @PostMapping("comments/{id}/dislike")
    public ResponseEntity<Boolean> cancelCommentLike(@PathVariable("id") String videoId) {
        //进行点赞
        return ResponseEntity.ok(videoService.cancelCommentLike(videoId));
    }

    /**
     * 关注
     *
     * @param toUserid 被关注的用户的id
     */
    @PostMapping("{uid}/userFocus")
    public ResponseEntity<String> attention(@PathVariable("uid") String toUserid) {
        Boolean attention = videoService.attention(toUserid);
        if (attention == null) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
        return ResponseEntity.ok("关注成功");
    }

    /**
     * 取消关注
     *
     * @param toUserid 被关注的用户的id
     */
    @PostMapping("{uid}/userUnFocus")
    public ResponseEntity<String> unsubscribe(@PathVariable("uid") String toUserid) {
        Boolean attention = videoService.unsubscribe(toUserid);
        if (attention == null) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
        return ResponseEntity.ok("关注成功");
    }
}

